package guilayer;

import java.awt.CardLayout;
import java.sql.SQLException;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import controllayer.*;

/**
 * @author Gruppe 3
 */
public class ViewAllOrdersPanel extends JPanel {
	private JTable orderTable;
	private JScrollPane scrollPane;
	private JPanel cardPanel, viewPanel;
	private SalesOrderCtr sOCtr;

	/**
	 * Create the panel.
	 */
	public ViewAllOrdersPanel() {
		sOCtr = new SalesOrderCtr();
		setLayout(null);
		initTable();
		initSeconPanel();
		initButtons();
	}
	
	/**
	 * Initialize JTable
	 */
	private void initTable() {
		
		String[][] data = sOCtr.salesOrderToTableArray();
		String[] column = {"ID", "Date", "Delivery status", "Delivery date", "Customer"};
		
		orderTable = new JTable(data, column){
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int column) {                
	                return false;               
	        }
	    };
		orderTable.getTableHeader().setReorderingAllowed(false);
		orderTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		orderTable.setFillsViewportHeight(true);
		orderTable.setAutoCreateRowSorter(true);
		orderTable.setVisible(true);
		scrollPane = new JScrollPane(orderTable);
        scrollPane.setBounds(10, 11, 445, 400);
        add(scrollPane);
        
        orderTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent event) {
            	try {
            		viewOrder(Integer.parseInt(orderTable.getValueAt(orderTable.getSelectedRow(), 0).toString()));
            	} catch(ArrayIndexOutOfBoundsException e) {
            	} catch (NumberFormatException e) {
					System.out.println(e);
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });

	}
	
	/**
	 * Initialize the secondPanel
	 */
	private void initSeconPanel() {
		cardPanel = new JPanel();
		cardPanel.setBounds(468, 11, 350, 434);
		add(cardPanel);
		cardPanel.setLayout(new CardLayout(0, 0));
	}
	
	public void initButtons() {
		JButton btnNewOrder = new JButton("New Order");
        btnNewOrder.setBounds(366, 422, 89, 23);
        add(btnNewOrder);
	}
	
	/**
	 * Updates the Table
	 */
	public void updateTable() {
		orderTable.removeAll();
		this.remove(orderTable);
		scrollPane.removeAll();
		this.remove(scrollPane);
		initTable();
	}
	
	/**
	 * Changes panel to viewForest
	 * @param forestName
	 * @throws SQLException 
	 */
	public void viewOrder(int orderID) throws SQLException {		
		viewPanel = new ViewOrderPanel(orderID);
		cardPanel.add(viewPanel);
		CardLayout cl = (CardLayout)(cardPanel.getLayout());
		cl.last(cardPanel);
	}
}
